using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Enemy_Base : MonoBehaviour
{

    public float tikTemp = 10f;


    public GameObject target; //追逐目标

    public Vector2 walkStartPos; //巡逻起始点
    public Vector2 walkEndPos; //巡逻终止点
    public float walkSpeed = 1.0f;
    public float traceSpeed = 1.0f;

    public float jumpForce = 3f; //跳跃力
    public float jumpThreshod = 1f; //高度差阈值，当与目标高度差超过此值时触发跳跃

    public BoxCollider2D detectRange;

    protected Rigidbody2D rb;


    //
    protected enum EnemyStatus{
        walking, tracing, returnPos
    }

    protected EnemyStatus status = EnemyStatus.walking;

    protected bool toEnd = true;

    // Start is called before the first frame update
    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
    }

    void OnEnable(){
        transform.position = walkStartPos;
        EventHandler.PlayerMimeticEvent += OnPlayerMimeticEvent;
    }

    // Update is called once per frame
    void Update()
    {
        if(tikTemp > 0){
            tikTemp -= Time.deltaTime;
        }
        if(tikTemp < 0){
            tikTemp = 0;
            EventHandler.CallPlayerMimeticEvent();
            Debug.Log("needReturn");
        }


        switch(status){
            case EnemyStatus.walking:
                OnWalk();
                break;
            case EnemyStatus.tracing:
                OnTrace();
                break;
            case EnemyStatus.returnPos:
                OnReturn();
                break;
        }
    }

    void OnPlayerMimeticEvent(){
        status = EnemyStatus.returnPos;
        Debug.Log("returnStartPos");
    }


    void OnTriggerEnter2D(Collider2D other){
        if(other.gameObject == target){
            status = EnemyStatus.tracing;
            Debug.Log("onTrace");
        }
    }

    void OnWalk(){
        Vector2 currentPos = transform.position;
        if(currentPos == walkStartPos){ 
            toEnd = true;
        }
        if(currentPos == walkEndPos){ 
            toEnd = false;
        }
        Vector2 targetPos = toEnd ? walkEndPos : walkStartPos;
        MoveForward(targetPos);
    }

    void OnTrace(){
        MoveForward(target.transform.position);
    }

    void OnReturn(){
        Vector2 currentPos = transform.position;
        if(currentPos == walkStartPos){
            status = EnemyStatus.walking;
            Debug.Log("onwalking");
            return;
        }
        MoveForward(walkStartPos);
    }

    void MoveForward(Vector2 targetPos){
        //水平移动
        float moveSpeed = (status == EnemyStatus.tracing) ? traceSpeed : walkSpeed; 
        Vector2 currentPos = transform.position;
        transform.position = Vector2.MoveTowards(currentPos, targetPos, moveSpeed * Time.deltaTime);
        
        //检测高度触发跳跃
        float yDiff = targetPos.y - currentPos.y;
        if(yDiff > jumpThreshod && IsGrounded()){ 
            rb.velocity = new Vector2(rb.velocity.x, jumpForce);
        }

        //根据方向变换Sprite
        float xDiff = targetPos.x - currentPos.x;
        Vector3 localScale = transform.localScale;
        if(xDiff < 0){
            transform.localScale = new Vector3(-Mathf.Abs(localScale.x), localScale.y, localScale.z);
        }else{
            transform.localScale = new Vector3(Mathf.Abs(localScale.x), localScale.y, localScale.z);
        }
    }

    bool IsGrounded(){
        return Mathf.Abs(rb.velocity.y) < 0.01f;
    }
}
